import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:yp/config/index.dart';
import 'package:yp/widgets/button_widget.dart';
import 'package:yp/widgets/index.dart';
import 'package:yp/widgets/switch.dart';

/// 新增地址 or 编辑地址

typedef OnItemTap<T> = void Function(T value);

class EditAddress extends StatefulWidget {
  const EditAddress({Key key}) : super(key: key);

  @override
  _EditAddressState createState() => _EditAddressState();
}

class _EditAddressState extends State<EditAddress>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  List<Map<String, dynamic>> _tags = [
    {'title': '家', 'isSelected': true},
    {'title': '公司', 'isSelected': false},
    {'title': '公司', 'isSelected': false}
  ];

  bool _flag = false;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: YPNavigationBar('新增地址'),
        body: SingleChildScrollView(
          padding: EdgeInsets.zero,
          child: Column(
            children: [
              _item('收货人', '填写收货人姓名'),
              _item('手机号', '填写11位手机号码', keyboardType: TextInputType.phone),
              _item('所在地区', '点击选择所在地区', onItemTap: (value) {
                print(value);
              }),
              _item('详细地址', '输入详细地址'),
              _tag(_tags),
              YpLine(),
              _setDefault(),
              YpLine()
            ],
          ),
        ),
        backgroundColor: EsColors.White,
        floatingActionButton: _bottom(),
        floatingActionButtonLocation:
            FloatingActionButtonLocation.centerDocked);
  }

  /// 普通的item
  Widget _item(String title, String tips,
      {OnItemTap onItemTap, TextInputType keyboardType}) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 12),
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 18, bottom: 12),
            child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SizedBox(
                    width: 80,
                    child: Text(
                      title,
                      style: TextStyle(
                          color: EsColors.Content,
                          fontSize: EsSizes.SM,
                          fontWeight: EsFontWeights.Normal),
                    ),
                  ),
                  Expanded(
                    child: TextField(
                      onTap: () {
                        print('点击事件');
                      },
                      style: TextStyle(
                        fontSize: EsSizes.SM,
                        color: EsColors.TitleSecondary,
                      ),
                      readOnly: onItemTap != null,
                      // enabled: onTap != null,
                      textDirection: TextDirection.ltr,
                      textAlign: TextAlign.end,
                      keyboardType: keyboardType ?? TextInputType.text,
                      decoration: InputDecoration(
                        hintText: tips,
                        isDense: true,
                        filled: false,
                        contentPadding: EdgeInsets.zero,
                        fillColor: Colors.transparent,
                      ),
                      inputFormatters: [LengthLimitingTextInputFormatter(50)],
                    ),
                  ),
                ]),
          ),
          YpLine(),
        ],
      ),
    );
  }

  ///  标签
  Widget _tag(List<Map<String, dynamic>> tagList) {
    return Padding(
      padding: const EdgeInsets.only(top: 18, bottom: 13, left: 12, right: 12),
      child: Row(
        children: [
          SizedBox(
            width: 80,
            child: Text(
              '标签',
              style: TextStyle(
                  color: EsColors.Content,
                  fontSize: EsSizes.SM,
                  fontWeight: EsFontWeights.Normal),
            ),
          ),
          Expanded(
            child: Wrap(
              alignment: WrapAlignment.end,
              spacing: 14,
              children: List.generate(
                  tagList.length,
                  (index) => EsOutLineButton(
                        tagList[index]['title'],
                        normal: !tagList[index]['isSelected'],
                        padding:
                            EdgeInsets.symmetric(vertical: 3, horizontal: 16),
                        onTap: () {
                          setState(() {
                            for (var i = 0; i < tagList.length; i++) {
                              tagList[i]['isSelected'] = i == index;
                            }
                          });
                        },
                      )),
            ),
          ),
        ],
      ),
    );
  }

  /// 默认
  Widget _setDefault() {
    return Padding(
      padding: const EdgeInsets.only(top: 10, bottom: 5, left: 12, right: 12),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          SizedBox(
            width: 80,
            child: Text(
              '标签',
              style: TextStyle(
                  color: EsColors.Content,
                  fontSize: EsSizes.SM,
                  fontWeight: EsFontWeights.Normal),
            ),
          ),
          EsSwitch(
            status: _flag,
            onChanged: (val) {
              print(val);
              setState(() {
                _flag = val;
              });
            },
          )
        ],
      ),
    );
  }

  /// 下边的按钮
  Widget _bottom() {
    return EsBottomWidget(
        leftTitle: null,
        rightTitle: '保存并使用',
        rightlick: () {
          Get.back();
        });
  }
}
